package ca.jsbr.canvaswar.games.mission 
{
	import ca.jsbr.as2js_lib.display.animations.Tween;
	import ca.jsbr.as2js_lib.display.canvas.Node2D;
	import ca.jsbr.as2js_lib.display.canvas.view.SpriteView;
	import ca.jsbr.as2js_lib.event.MouseJSEvent;
	import ca.jsbr.as2js_lib.utils.assets.AssetsFactory;
	import ca.jsbr.as2js_lib.utils.MouseManager;
	import ca.jsbr.canvaswar.games.map.Map;
	import ca.jsbr.canvaswar.games.map.Tile;
	import ca.jsbr.canvaswar.games.ui.UiLayer;
	import ca.jsbr.canvaswar.games.unit.Unit;
	import flash.events.Event;
	import flash.utils.Dictionary;
	/**
	 * ...
	 * @author jsbr [jsricard@lvlstudio.com]
	 */
	public class Mission extends Node2D
	{
		private var _unit:Unit;
		protected var _ready:Boolean;
		protected var _map:Map;
		protected var _ui:UiLayer;
		protected var _userFaction:String = "blue";
		protected var _selectedTile:Tile;
			
		public function Mission()
		{
			_ui = new UiLayer();
		}
		
		public function init():void
		{
			MouseManager.getInstance().addEventListener(MouseJSEvent.MOUSE_CLICK, onMouseClick);
			_ui.startMouse();
		}
		
		
		
		
		public function start():void
		{
			addChild(_ui);
			
		}
		
		public function get ready():Boolean 
		{
			return _ready;
		}
		
		
		private function onMouseClick(e:MouseJSEvent):void 
		{
			if (_ui.freeze)
				return
			var tile:Tile = _map.getTileByCoordinate(e.stageX, e.stageY);
			
			
			if (tile.overlay && tile.overlay.currentPlay)
			{
				var path:Vector.<Tile> = _map.getPath(_unit.tile, tile);
				if (tile.overlay && tile.overlay.currentPlay == "attack")
					path.shift();
				
				_ui.closeToolTips();
				_unit.moveTo(path);
				_unit = null;
				if (tile.overlay && tile.overlay.currentPlay == "attack")
					_ui.showBattleMenu(_unit, tile.unit);
				_map.clearOverlay();
			}
			else if (tile.unit)
			{
				_unit=tile.unit
				var spriteMove:SpriteView = new SpriteView(AssetsFactory.getInstance().getAssets("tile_overlay"));
				spriteMove.play("move");
				var spriteAttack:SpriteView = spriteMove.clone();
				spriteAttack.play("attack");
				var tiles:Vector.<Tile> = _map.showTileMove(tile.unit, spriteMove, spriteAttack);
				showInfos(tile);
			}
			else
				showInfos(tile);
			_selectedTile = tile;
		}
		
		private function showInfos(tile:Tile):void 
		{
			var arr:Array = [];
			
			arr.push("tile", "|");
			arr.push("defence", "1");
			if (tile.unit)
			{
				arr.push("unit", "|");
				arr.push("healt", "10");
				arr.push("resistance", "3");
				arr.push("force", "10");
			}
			
			
			_ui.showToolTips(tile.asset.x + tile.asset.width * 0.5, tile.asset.y + tile.asset.width * 0.5, arr);
		}
		
		
	}

}